package com.coco.user.controller;

import com.coco.common.annotation.IngoreResponseAdvice;
import com.coco.common.result.CommonResponse;
import com.coco.common.result.ResultData;
import com.coco.user.pojo.TbUser;
import com.coco.user.service.UserService;
import com.coco.user.vo.UserLoginVo;
import com.coco.user.vo.UserRegisterVo;
import com.coco.user.vo.UserSearchVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: coco
 * @author: Mr.Lin
 * @create: 2021-07-21 20:09
 **/
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户列表
     * @return
     */
    @GetMapping("/userList")
    public List<TbUser> userList(){
        return userService.userList();
    }

    @GetMapping("/findOne/{id}")
    @IngoreResponseAdvice
    public TbUser findOne(@PathVariable("id") Long id){
        return userService.findOne(id);
    }

    /**
     * 增加
     * @param user
     * @return
     */
    @PostMapping("/add")
    public void add(@RequestBody TbUser user){
        try {
            userService.add(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改
     * @param user
     * @return
     */
    @PutMapping("/update")
    public void update(@RequestBody TbUser user){
        try {
            userService.update(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 删除
     *
     * @param id
     */
    @DeleteMapping("/deleteById/{id}")
    public void  deleteById(@PathVariable("id") Long id){
        userService.deleteById(id);
    }


    /**
     * 查询
     *
     * @param
     * @return
     */
    @PostMapping("/search")
    public ResultData search(@RequestBody UserSearchVo userSearchVo ){
        return userService.findAllSearch(userSearchVo);
    }


    /**
     * 用户注册
     * @param userRegisterVo
     * @return
     */
    @PostMapping("/register")
    public CommonResponse register(@RequestBody UserRegisterVo userRegisterVo){
        return userService.register(userRegisterVo);
    }


    /**
     *@描述   获取验证码
     *@参数  [phone]
     *@返回值  void
     *@创建人  zhanglongfei
     *@创建时间  2021/7/16
     *@修改人和其它信息
     */
    @GetMapping("/getCode")
    public void getCode (@RequestParam String phone){
        userService.getCode(phone);
    }

    /**
     * 前台用户登录
     */
    @PostMapping("/dologin")
    public CommonResponse dologin(@RequestBody UserLoginVo loginVo){
        return userService.dologin(loginVo);
    }




}
